property spritestring -- the string that is set by the user, a list without brackets
property snapList -- the list that is built from spritestring, the snap targets
property snapDistance -- how many pixels from the reg point of targets to snap
property xoffset, yoffset -- maintains offfset of cursor after mouse down
property snapboxes -- a list [sprite, [top: , bottom: , left: , right]],...
property tracking -- if the mouse is now dragging, true
property currentboxsprite -- 0 if not in any box, otherwise snapped to sprite
property boxindex -- position in snapboxes of currentbox
on beginsprite me
set snapboxes = []
set the snaplist of me = convertstring (spriteString)
repeat with isprite in snaplist
set boxrecord = [ #top:the locv of sprite isprite - snapDistance, #bottom : the locv of sprite isprite + snapDistance, #left : the loch of sprite isprite - snapDistance, #right : the loch of sprite isprite + snapDistance ]
add snapboxes, [isprite,boxrecord]
end repeat
end
on prepareframe me
if tracking then
if currentboxsprite = 0 then
-- tracking and not yet snapped
if inanybox (me, the mouseH - xoffset, the mouseV - yoffset) then
-- tracking, in a box, snap it
snap me, currentboxsprite
exit
else
-- tracking, not in a box, drag it
set the locH of sprite the spritenum of me = the MouseH - xoffset
set the locV of sprite the spritenum of me = the MouseV - yoffset
exit
end if
end if
-- snapped, tracking, check that mouse is still in the box
if inabox (me, the mouseH - xoffset, the mouseV - yoffset, boxindex) then
-- snapped, tracking, and still in the box
-- don't move the thing, it's still snapped
exit
else
-- was snapped, now outside of box, drag it
set the locH of sprite the spritenum of me = the MouseH - xoffset
set the locV of sprite the spritenum of me = the MouseV - yoffset
exit
end if
end if
end
on mousedown me
set tracking = TRUE
set xoffset = the mouseH - the locH of sprite the spritenum of me
set yoffset = the mouseV - the locV of sprite the spritenum of me
end
on mouseup me
set tracking = FALSE
end
on snap me, snaptarget
set the locH of sprite the spritenum of me = the locH of sprite snapTarget
set the locV of sprite the spritenum of me = the locV of sprite snapTarget
end
on inanybox me,x,y
-- sets currentboxsprite to spritenum of the box the cursor is in, 0 if no box
-- returns TRUE if inanybox, false otherwise
set index = 0
repeat with ibox in snapboxes
set index = index + 1
set boxvals = getAt(ibox, 2)
if (x < (getprop (boxvals, #right))) and (x > (getprop (boxvals, #left))) and (y < (getprop (boxvals, #bottom))) and (y > (getprop (boxvals, #top))) then
set currentboxsprite = getAt (ibox, 1)
set boxindex = index
return 1
end if
end repeat
set boxindex = 0
set currentboxsprite = 0
return 0
end
on inabox me,x,y,boxindex
-- returns TRUE if in boxindex box, false otherwise
-- used for tacking if the mouse leaves the current snap box
set ibox = getAt(snapboxes, boxindex)
set boxvals = getAt(ibox, 2) -- boxvals is a propertylist [top:,bottom:,etc.]
if x < getprop (boxvals, #right) and x > getprop (boxvals, #left) and y < getprop (boxvals, #bottom) and y > getprop (boxvals, #top) then
return "Cause a sprite to snap to the position of any of a list of sprites while dragging." & RETURN & "PARAMETERS:" & RETURN & "ò Sprite List - Enter the channel numbers of sprites to snap to, separated by commas." & RETURN & "ò Snap Distance - Enter the range in pixels within which snapping will occur. If set to zero, the sprite being dragged always snaps to the location of the nearest list member."